<?php 
namespace Users\Controller;

use Admin\Controller\AdminbaseController;
use Teacher\Controller\TeacheradminController;
class AdminsadminController extends AdminbaseController{
	
	protected $m;
	
	function _initialize() {
		parent::_initialize();
		$this->m = M('Users');
	}
	
	/**
	 * @name 管理员列表
	 * @auth adminleft
	 */
	public function index(){
		$this->display();
	}
	
	public function edit(){
		if($_GET['id']){
			$info = $this->m->find($_GET['id']);
			$map['group'] = 'admin';
			$map['uid'] = $_GET['id'];
			
			$role = M('AuthGroupAccess')->where($map)->find();
			$info['role_id'] = $role['group_id'];
			
			$this->info = $info;
		}
		$gmap['group'] = 'admin';
		$this->groups = M('AuthGroup')->where($gmap)->field('id,title,desc')->select();
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$_POST['update_time'] = Date('Y-m-d H:i:s');
// 			dump($_POST);
// 			exit();
			if($_POST['uid']){
				if($_POST['password']){
					$_POST['password'] = md5($_POST['password']);
				}else{
					unset($_POST['password']);
				}
				if($this->m->save($_POST)){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}else{
				$_POST['create_time'] = $_POST['update_time'];
				if(!$_POST['password']){
					$this->error("请输入密码");
					exit();
				}else{
					$_POST['password'] = md5($_POST['password']);
				}
				if($this->m->add($_POST)){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}
			
		}
	}
	
	public function editinfo(){
		$uid = $this->curUser['uid'];
		$this->info = D('Users')->relation(true)->find($uid);
		$this->display(); 
	}
	
	public function editinfo_post(){
		if(IS_POST){
			$data = array(
				'uid' => $_POST['uid'],
				'realname' => $_POST['realname'],
				'sex' => $_POST['sex'],
				'email' => $_POST['email'],
				'update_time'=> Date('Y-m-d H:i:s')
			);
			if($this->m->save($data)){
				R('Teacher/Teacheradmin/save_english',array('uid'=>$_POST['uid'],'englis'=>$_POST['english']));
				$this->success('保存成功',U('editinfo'));
			}else{
				$this->error('保存失败');
			}
		}
	}
	
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('uid' => array('in', $id));
		$mapp = array('uid' => array('in', $id));
		$mapp['group'] = 'admin';
		if($this->m->where($map)->delete()){
			M('AuthGroupAccess')->where($mapp)->delete();
			$this->success('删除成功',U('index'));
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function changestatus(){
		$data['uid'] = $_REQUEST['id'];
		$data['status'] = $_REQUEST['status'];
		$data['update_time'] = date('Y-m-d H:i:s');
		if($this->m->save($data)){
			$this->success('状态更新成功');
		}else{
			$this->error('状态更新失败');
		}
	}
	
	/*
	 * 检查用户名唯一
	 */
	public function checkname(){
		$map['username'] = $_REQUEST['username'];
		$map['uid'] = array('not in',$_REQUEST['id']);
		$user = $this->m->where($map)->find();
		if($user){
			echo  '{"error":"管理员名已经存在"}';
		}else{
			echo '{"ok":"管理员名可用"}';
		}
	}
	
	public function check(){
		$t = I('get.t');
		if($t === 'mobile'){
			$value = $_REQUEST['mobile'];
			$str_ok = '';
			$str_error = '手机号码已存在';
		}
		$uid = I('get.uid');
		if($uid){
			$map['uid'] = array('neq',$uid);
		}
		$map[$t] = $value;
		if(!$this->m->where($map)->find()){
			$data['ok'] = $str_ok;
		}else{
			$data['error'] = $str_error;
		}
		$this->ajaxReturn($data,'JSON');
	}
	
	/**
	 * @name 重置密码
	 * @auth adminleft
	 */
	public function resetpwd(){
		$this->display();
	}
	
	public function resetpwd_post(){
		if(IS_POST){
			$_POST['uid'] = $this->curUser['uid'];
			$_POST['password'] = md5($_POST['password']);
			$_POST['update_time'] = date('Y-m-d H:i:s');
			$this->m->create();
			if($this->m->save()){
				$this->success('密码重置成功',U('resetpwd'));
			}else{
				$this->error('密码重置失败');
			}
		}
	}
	
	public function checkoldpwd(){
		$user = $this->m->find($this->curUser['uid']);
		if($user['password'] === md5(trim($_REQUEST['oldpassword']))){
			echo '{"ok":"原密码正确"}';
		}else{
			echo  '{"error":"原密码不正确"}' ;
		}
	}
	
	//普通会员列表
	/**
	 * @name 会员列表
	 * @auth adminleft
	 */
	public function mlist(){
		if(!$_GET['status']){
			$_GET['status'] = 0;
		}
		$this->display();
	}
	
	public function getrolename($id){
		return M('AuthGroup')->where('id = '.$id)->getField('title');
	}
}
?>